package dragon.modules.system.menu.action;

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import dragon.core.db.DBUtil;
import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysMenu;
import dragon.core.util.Handler;
import dragon.core.util.StringUtils;
import dragon.modules.system.menu.dao.MenuDaoImpl;
import ejp.Database;

/**
 * Menu
 */
public class MenuHandler extends Handler {

	MenuDaoImpl dao = new MenuDaoImpl();

	/**
	 * 停、启用
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeState() throws ExceptionHandler {
		String state = getParameter("state");

		// 获取菜单ID
		String[] ids = null;
		JSONArray rows = null;
		JSONObject jsonObj = null;
		try {
			rows = (JSONArray) new JSONParser().parse(getParameter("rows"));
			ids = new String[rows.size()];
			int i = 0;
			for (Object obj : rows) {
				jsonObj = (JSONObject) obj;
				ids[i++] = jsonObj.get("ID").toString();
			}
			jsonObj = null;
			rows = null;
		} catch (ParseException e) {
			jsonObj = null;
			rows = null;
			ids = null;
			throw new ExceptionHandler(e);
		}

		if (ids != null) {
			Database db = null;
			try {
				db = DBUtil.getDatabase();
				DML.beginTransaction(db);// 开启事务
				for (String id : ids) {
					SysMenu menu = dao.get(db, Integer.parseInt(id));
					if (state.equals("1")) {
						// 启用需要同步启用其所有父级节点
						List<String> list = new ArrayList<String>();
						String idPath = menu.getIdpath();
						list.add(idPath);
						int lastIndex = 0;
						while ((lastIndex = idPath.lastIndexOf("/")) != 0) {
							idPath = idPath.substring(0, lastIndex);
							list.add(idPath);
						}
						idPath = null;

						StringBuilder sb = new StringBuilder();
						for (String s : list) {
							sb.append(s).append("','");
						}
						list = null;
						if (sb.length() > 0) {
							sb.delete(sb.length() - 3, sb.length());
						}

						DML.executeUpdate(db, "update " + SysMenu.tableName + " set state='1' where idpath in ('" + sb.toString() + "')");
						sb = null;
					} else {
						// 停用操作需要同步停用其下所有子节点
						DML.executeUpdate(db, "update " + SysMenu.tableName + " set state='0' where idpath like '" + menu.getIdpath() + "%'");
					}
					menu = null;
				}
				DML.endTransaction(db);// 关闭事务
			} catch (ExceptionHandler e) {
				if (db != null) {
					try {
						DML.rollback(db);// 回滚事务
					} catch (ExceptionHandler e1) {
						throw e1;
					}
				}
				throw e;
			}
			output(successJSON);
		}
		ids = null;
	}

	/**
	 * 菜单移动
	 * 
	 * @throws ExceptionHandler
	 */
	public void changeParent() throws ExceptionHandler {
		String id = getParameter("id");// 待移动的部门ID
		String target = getParameter("target");// 目标父节点ID
		if ("null".equalsIgnoreCase(target)) {
			target = "0";
		}
		if (StringUtils.isInteger(id) && StringUtils.isInteger(target)) {
			new MenuDaoImpl().changeParent(Integer.parseInt(id), Integer.parseInt(target));
		}
		output(successJSON);
	}

	/**
	 * 将菜单载入缓存
	 * 
	 * @throws ExceptionHandler
	 */
	public void load2Ram() throws ExceptionHandler {
		MenuDaoImpl.cache();
		output(successJSON);
	}

}